package com.yanqu.road.dao.impl.activity.musicroomrank;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.musicroomrank.CrossMusicRoomRankRoundData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;

public class CrossMusicRoomRankRoundDaoImpl extends TempDataObjectDao<CrossMusicRoomRankRoundData> {
    public static String getCrossMusicRoomRankRoundDataSql = "select * from `t_c_music_room_rank_round` where `activityId`=? order by round desc limit ?";

    public List<CrossMusicRoomRankRoundData> getCrossMusicRoomRankRoundList(int activityId, int maxNum) {
        List<CrossMusicRoomRankRoundData> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossMusicRoomRankRoundDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, maxNum);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossMusicRoomRankRoundData data = parseData(rs);
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }


    public static String updateCrossMusicRoomRankRoundDataSql = "update `t_c_music_room_rank_round` set theme=?,startTime=?,finishTime=?,waitSettleServerSet=? where activityId=? and round=?";
    @Override
    protected boolean updateData(CrossMusicRoomRankRoundData crossMusicRoomRankRoundData) {
        crossMusicRoomRankRoundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getTheme()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankRoundData.getStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankRoundData.getFinishTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossMusicRoomRankRoundData.getWaitSettleServerSet())));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getRound()));
        if (getDb().executeNoneQuery(updateCrossMusicRoomRankRoundDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossMusicRoomRankRoundDataSql = "insert into `t_c_music_room_rank_round`(activityId,round,theme,startTime,finishTime,waitSettleServerSet) values (?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossMusicRoomRankRoundData crossMusicRoomRankRoundData) {
        crossMusicRoomRankRoundData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, crossMusicRoomRankRoundData.getTheme()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankRoundData.getStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossMusicRoomRankRoundData.getFinishTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossMusicRoomRankRoundData.getWaitSettleServerSet())));
        if (getDb().executeNoneQuery(insertCrossMusicRoomRankRoundDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossMusicRoomRankRoundData parseData(ResultSet rs) throws SQLException {
        CrossMusicRoomRankRoundData crossMusicRoomRankRoundData = new CrossMusicRoomRankRoundData();
        crossMusicRoomRankRoundData.setActivityId(rs.getInt("activityId"));
        crossMusicRoomRankRoundData.setRound(rs.getInt("round"));
        crossMusicRoomRankRoundData.setTheme(rs.getInt("theme"));
        crossMusicRoomRankRoundData.setStartTime(rs.getLong("startTime"));
        crossMusicRoomRankRoundData.setFinishTime(rs.getLong("finishTime"));
        String waitSettleServerSet = rs.getString("waitSettleServerSet");
        if (!StringUtils.isNullOrEmpty(waitSettleServerSet)){
            crossMusicRoomRankRoundData.setWaitSettleServerSet(JSONObject.parseObject(waitSettleServerSet, new TypeReference<HashSet<Long>>(){}));
        }

        return crossMusicRoomRankRoundData;
    }
}
